<template>
  <view class="container">
		<view style="height: 10rpx;background-color: #fff"></view>
		<view class="notify-item" @click="$goPage('/note/system/catalogue',{type:1, notifyType: 2})">
			<view class="notify-item-left">
				<view class="icon icon1">
					<i class='icon icon-fangzi'></i>
				</view>
				<view class="content">
					<text class="content-title">{{ $t('chat.bbs_notify') }}</text>
					<text class="content-text">{{ bbsLastMsg }}</text>
				</view>
			</view>
			<view class="notify-item-right">
			  <view class="badge" v-if="bbsCount > 0">{{ bbsCount }}</view>
				<u-icon name="arrow-right" color="#a7a7af" size="36"></u-icon>
			</view>
		</view>
		<view class="notify-item" @click="$goPage('/note/system/catalogue',{type: 1, notifyType: 1})">
			<view class="notify-item-left">
				<view class="icon icon2">
					<i class='icon icon-shebeixiuli'></i>
				</view>
				<view class="content">
					<text class="content-title">{{ $t('chat.update_repair') }}</text>
					<text class="content-text">{{ repairLastMsg }}</text>
				</view>
			</view>
			<view class="notify-item-right">
			  <view class="badge" v-if="repairCount > 0">{{ repairCount }}</view>
			  <u-icon name="arrow-right" color="#a7a7af" size="36"></u-icon>
			</view>
		</view>
		<view class="notify-item" @click="$goPage('/note/system/catalogue',{type: 1, notifyType: 0})">
			<view class="notify-item-left">
				<view class="icon icon3">
					<i class='icon icon-01_daiqueren'></i>
				</view>
				<view class="content">
					<text class="content-title">{{ $t('chat.repair_confirm') }}</text>
					<text class="content-text">{{ confirmLastMsg }}</text>
				</view>
			</view>
			<view class="notify-item-right">
			  <view class="badge" v-if="confirmCount > 0">{{ confirmCount }}</view>
			  <u-icon name="arrow-right" color="#a7a7af" size="36"></u-icon>
			</view>
		</view>
	  <view style="height: 10rpx;background-color: #fff"></view>
	</view>
</template>

<script>
import pageMixin from '@/utils/pageMixin';
export default {
  mixins: [pageMixin],
  data() {
    return {
      bbsCount: 0,
			bbsLastMsg: '',
			repairCount: 0,
			repairLastMsg: '',
			confirmCount: 0,
			confirmLastMsg: '',
    }
  },

  onLoad(option) {
		this.$store.commit('checkUserLogin');
    uni.setNavigationBarTitle({
      title: this.$t('chat')['notice_title']
    });
  },
  onShow() {
   uni.setNavigationBarTitle({
     title: this.$t('chat')['notice_title']
   });
	this.getList()
  },
  components: {},
  computed: {
    i18n() {
      return this.$t('chat')
    }
  },
  methods: {
    getList() {
			this.confirmLastMsg = ''
			this.repairLastMsg = ''
			this.bbsLastMsg = ''
			this.$http.get('/msg/system/list/data').then(res=> {	
				console.log(res)
				this.confirmCount = 0
				this.repairCount = 0
				this.bbsCount = 0
				var data = this.getTypeList(res)
				console.log(data)
				 data[0].forEach(item=> {
					 if(item.ReadTime == 0) {
						 this.confirmCount++  //累计未读消息数
					 }
				 })
				 data[1].forEach(item=> {
				 		if(item.ReadTime == 0) {
				 			this.repairCount++
				 		}
				 })
				 data[2].forEach(item=> {
				 		if(item.ReadTime == 0) {
				 			this.bbsCount++
				 		}
				 })
				 for (let i = 0; i < 3; i++) {
					 if(data[i].length > 0) {
						let type = data[i][0].Type || 0;
						  let bizType = data[i][0].BizType || 0;
						  let content = data[i][0].Content;
											 console.log(data[i][0])
						  if (this.$t('chat')["msg_title_" + bizType] !== undefined) {
						    data[i][0].Title = this.$t('chat')["msg_title_" + bizType];
						  }
						  if (this.$t('chat')["msg_content_" + bizType] !== undefined && type != 2) {
						    //let number = res[i].Content.replace(/[^0-9]/ig, "");
						    let number = (data[i][0].Content.match(/\d+个工程/g)+"").match(/\d+/g);
						    content = this.$t('chat')["msg_content_" + bizType];
						    if (this._i18n.locale == '中文') {
						      content = content.replace('[projectName]', data[i][0].ProjectName)
						    } else {
						      content = content.replace('[projectName]', data[i][0].ShipName)
						    }
						    content = content.replace('[userName]', data[i][0].ExtDataJson.AddAccName || '')
						    content = content.replace('[number]', number)
						    data[i][0].Content = content;
						  }
						
						  //确认结果状态，2：确认、3：驳回
						  let stat =data[i][0].ExtDataJson.Stat || 0;
						  //alert(res[i].ExtDataJson.stat+"-----"+stat);
						  if (stat == 3) {
						    let rejectRmk = data[i][0].ExtDataJson.RejectRmk || '';
						    if (rejectRmk != '') {
						      content = content.replace('[rejectRmk]', rejectRmk)
						      data[i][0].Content = content;
						    }
						  } else if (stat == 2) {
						    if (this.$t('chat')["msg_content_" + bizType + "_confirm"] !== undefined) {
						      //let number = res[i].Content.replace(/[^0-9]/ig, "");
						      let number = (data[i][0].Content.match(/\d+个工程/g)+"").match(/\d+/g);
						      let content = this.$t('chat')["msg_content_" + bizType + "_confirm"];
						      if (this._i18n.locale == '中文') {
						        content = content.replace('[projectName]', data[i][0].ProjectName)
						      } else {
						        content = content.replace('[projectName]', data[i][0].ShipName)
						      }
													 content = content.replace('[userName]', data[i][0].ExtDataJson.AddAccName || '')
						      content = content.replace('[number]', number)
						      data[i][0].Content = content;
						    }
												 }
												 //修理单结果通知
												 								if(bizType == 16) {
												 									let amount = data[i][0].Content.slice(data[i][0].Content.indexOf('为') + 1,data[i][0].Content.indexOf('详')-1)
												 									let content = this.$t('chat')["msg_content_16"];
												 							
												 									if (this._i18n.locale == '中文') {
												 									  content = content.replace('[projectName]', data[i][0].ProjectName)
												 									} else {
												 									  content = content.replace('[projectName]', data[i][0].ShipName)
												 									}
												 									content = content.replace('[amount]',amount)
												 									data[i][0].Content = content;							
												 								 }
												 								 //好友@通知
												 								if(data[i][0].Title == '好友@通知'){
												 									let username = data[i][0].Content.slice(data[i][0].Content.indexOf('友')+1,data[i][0].Content.indexOf('在'))
												 								
												 									data[i][0].Title = this.$t('chat')["msg_@_notify"];
												 									let content = this.$t('chat')["msg_content_@"]
												 							
												 									content = content.replace('[userName]', username)
												 									data[i][0].Content = content
												 								}
												 								//好友关注
												 								if(bizType == 31) {
												 									let username = data[i][0].Content.slice(0,data[i][0].Content.indexOf('关'))
												 									let content = this.$t('chat')["msg_content_31"];
												 								
												 									content = content.replace('[userName]',username)
												 									data[i][0].Content = content;							
												 								 }
												 								 if(bizType == 32) {
												 									 let username = data[i][0].Content.slice(0,data[i][0].Content.indexOf('评'))
												 									 let content = this.$t('chat')["msg_content_32"];
												 									 content = content.replace('[userName]',username)
												 									 data[i][0].Content = content;			
												 								 }
												 								 //发布文章，所有粉丝发送消息至铃铛
												 								  if(bizType == 33) {
						                                                		let username = data[i][0].Content.slice(0,data[i][0].Content.indexOf('评'))
						                                                		let content = this.$t('chat')["msg_content_32"];
						                                                		content = content.replace('[userName]',username)
						                                                		data[i][0].Content = content;
						                                                }
						}
						if(data[0].length >0) {
							console.log('aa')
							this.confirmLastMsg = data[0][0].Content
						}
						if(data[1].length >0) {
							this.repairLastMsg = data[1][0].Content
						}
						if(data[2].length >0) {
							this.bbsLastMsg = data[2][0].Content 
						}
					 }
				  
			})
		},
		getTypeList(list) {
			console.log(list)
			var data = {
				0: [],
				1: [],
				2: []
			}
			if(list.length >0) {
				list.forEach(item=> {
					if(item.BizType >=11 && item.BizType <= 15 ) {
						data[0].push(item)
					}else if(item.BizType == 16) {
						data[1].push(item)
					}
					else if(item.BizType == 33 || item.BizType == 31 || item.BizType == 32 || item.BizType == 0 && item.Title != '需求指派通知') {
						data[2].push(item)
					}else{}
				})
			}
			
			return data
		}
  }
}
</script>

<style lang="scss" scoped>
.container {
	width: 100%;
	min-height: 100vh;
	background-color: #f1f1f1;
}
.notify-item {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 20rpx 32rpx;
	background-color: #fff;
	.notify-item-left {
		display: flex;
		align-items: center;
		.icon {
			width: 110rpx;
			height: 110rpx;
			border-radius: 110rpx;	
			text-align: center;
			line-height: 110rpx;
			color: #fff;
			font-size:70rpx;
		}
		.icon1 {
			background-image: linear-gradient(to bottom, #EB5690, #F00059);
		}
		.icon2 {
			background-image: linear-gradient(to bottom, #F3A158, #EB5715);
		}
		.icon3 {
			background-image: linear-gradient(to bottom, #4C9DFA, #2030DA);
		}
	  .content {
			display: flex;
			flex-direction: column;
			margin-left:20rpx;
			.content-title {
				font-size: 34rpx;
				font-weight: 700;
			}
			.content-text {
				font-size: 30rpx;
				color: #a7a7af;
				font-weight: 400;
				white-space: nowrap;
				width: 450rpx;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}
	}
	.notify-item-right {
		display: flex;
		align-items: center;
		.badge {
			width: 36rpx;
			height: 36rpx;
			margin-right: 6rpx;
			border-radius: 36rpx;
			text-align: center;
			line-height: 36rpx;
			background-color: #F01C28;
			color: #fff;
			font-size: 26rpx;
		}
	}
}
</style>
